package com.nhjf.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

import com.nhjf.common.util.Properties.SysProperties;

public class ProductPicUtil  {
	String sysSeparator ;  // windows是\，unix是/
	String fileUpload;
	String tmpPath ;// 临时文件目录
	String shopPath; // 商品保存的目录地址
	String imageType;
	String activityPicturePath; //首页板块图片保存路径
	String upload;
	String specialTopicPicturePath; //专题活动图片保存路径
	
	
	public String getActivityPicturePath() {
		return activityPicturePath;
	}
	public void setActivityPicturePath(String activityPicturePath) {
		this.activityPicturePath = activityPicturePath;
	}
	public  ProductPicUtil(){
		  sysSeparator = File.separator; // windows是\，unix是/ 
		  upload= SysProperties.getInstance().get("fileUploadPath")+sysSeparator+"upload"+sysSeparator;
		  fileUpload =SysProperties.getInstance().get("fileUploadPath")+sysSeparator+ "fileUpload" + sysSeparator;
		  tmpPath = fileUpload + "tmp"; // 临时文件目录
		  shopPath = fileUpload + "shop"; // 商品保存的目录地址
		  imageType = ".jpg";
		  
		  activityPicturePath=fileUpload + "activity"; //首页板块图片保存路径 
		  specialTopicPicturePath = fileUpload  + "specialTopic"; //专题活动图片保存路径
		  
	}
	
	/** 组建保存图片路径 */
	public String getProductPicFilePath(Long prodId,String fileName){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(shopPath).append(sysSeparator).append("product").append(sysSeparator).append(prodId).append(sysSeparator).append(fileName);
        return picFilePath.toString();
		
	}
	/** 组建保存评论图片路径 */
	public String getCommentPicFilePath(String commentId,String fileName){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(shopPath).append(sysSeparator).append("comment").append(sysSeparator).append(commentId).append(sysSeparator).append(fileName);
        return picFilePath.toString();
		
	}
	
	public String getProductPath(Long prodId){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(shopPath).append(sysSeparator).append("product").append(sysSeparator).append(prodId);
        return picFilePath.toString();
    }
	
	
	/** 首页板块图片保存路径 zh*/
	public String getActivityPicturePath(Long prodId){
		StringBuffer activityFilePath= new StringBuffer();
		activityFilePath.append(activityPicturePath).append(sysSeparator).append(prodId);
        return activityFilePath.toString();
    }
	
	/**专题活动图片保存路径*/
	public String getSpecialTopicPicturePath(Long specialTopicId){
		StringBuffer specialTopicPicturePath = new StringBuffer();
		specialTopicPicturePath.append(specialTopicPicturePath).append(sysSeparator).append(specialTopicId);
		return specialTopicPicturePath.toString();
	}
	
	
	public String getWebRootProductPath(HttpServletRequest request ,Long prodId){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(SysProperties.getInstance().get("imagePath")).append("/").append("shop").append("/").append("product").append("/").append(prodId);
        return picFilePath.toString();
	}
	
	public boolean mkdirForProduct(Long prodId){
		       boolean flag=true;
		       StringBuffer picFileDir= new StringBuffer();
		        picFileDir.append(shopPath).append(sysSeparator).append("product").append(sysSeparator).append(prodId);
                File fileDir = new File(picFileDir.toString());
				if (!fileDir.exists()) {// 存在该目录，则删除
				   flag = fileDir.mkdir();
				}
				return flag;
	}
	public boolean mkdirForComment(String commandId){
	        boolean flag=true;
	        StringBuffer picFileDir= new StringBuffer();
	        picFileDir.append(shopPath).append(sysSeparator).append("comment").append(sysSeparator).append(commandId);
	        File fileDir = new File(picFileDir.toString());
			if (!fileDir.exists()) {// 存在该目录，则删除
			   flag = fileDir.mkdir();
			}
			return flag;
}
	
	
	/**
	 * 删除图片文件夹
	 * @param f
	 * @return
	 */
	public boolean deletedir(File f) {
		if (f.isDirectory()) {
			File[] files = f.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()){
					deletedir(files[i]);
				}else{
					deletefile(files[i]);
				}
			}
		}
		f.delete();
		return true;

	}
	/**
	 * 删除文件
	 * @param f
	 * @return
	 */
	private boolean deletefile(File f) {
		if (f.isFile())
			f.delete();
		return true;
	}
	/**
	 * 上传文件到临时文件夹里
	 * @param file       文件
	 * @param FileName   保存图片的路径
	 * @return
	 */
	public String uploadFileToTmp(File file,String FileName){
		StringBuffer sb=new StringBuffer();
	    String imageTmpPath=sb.append(tmpPath).append(sysSeparator).append(FileName).toString();
		File fileObj=new File(imageTmpPath);
		fileObj.getParentFile().mkdirs();
		if(fileObj.exists()){
			fileObj.delete();
		}
	    try {
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
			BufferedOutputStream ou = new BufferedOutputStream(new FileOutputStream(new File(imageTmpPath)));
			byte[] buffer = new byte[64 * 1024];
			int length;
			while ((length = in.read(buffer, 0, buffer.length)) > 0) {
				ou.write(buffer, 0, length);
			}
			in.close();
			ou.flush();
			ou.close();
		} catch (Exception e) {
			LogMessage.logwrite(this.getClass(), e.getMessage());
		}
		return imageTmpPath ;
	}
	
	public String uploadFileToTmp(MultipartFile file,String FileName){
		StringBuffer sb=new StringBuffer();
	    String imageTmpPath=sb.append(tmpPath).append(sysSeparator).append(FileName).toString();
	    File fileObj = new File(imageTmpPath);  
		if(!fileObj.getParentFile().exists()) {
			fileObj.getParentFile().mkdirs();
		}
		if(fileObj.exists()){
			fileObj.delete();
		}
		System.out.println(file.getSize());
		System.out.println(fileObj.getAbsolutePath());
        //保存  
        try {  
        	file.transferTo(fileObj);
		} catch (Exception e) {
			e.printStackTrace();
			LogMessage.logwrite(this.getClass(), e.getMessage());
		}
		return imageTmpPath ;
	}
	/**
	 * 复制文件
	 * @param filePath
	 * @param destPath
	 */
	public static void copyFile(String filePath , String destPath) {
		File srcFile = new File(filePath);
		File destFile = new File(destPath);
		FileInputStream input = null;
		FileOutputStream output = null; 
		try {
			  input = new FileInputStream(srcFile);
			  output = new FileOutputStream(destFile);
			  byte[] buffer = new byte[4096];
              int n = 0;
              while (-1 != (n = input.read(buffer))) {
				      output.write(buffer, 0, n);
			  }
		} catch (FileNotFoundException e) {
			LogMessage.logwrite(ProductPicUtil.class, e.getMessage());
		}catch (IOException e) {
			LogMessage.logwrite(ProductPicUtil.class, e.getMessage());
		} finally {
			 try {
            	   if (output != null) {
                        output.close();
					}
                    if (input != null) {
                        input.close();
                    }
			 } catch (IOException e) {
				 LogMessage.logwrite(ProductPicUtil.class, e.getMessage());
		    }      
               
         }
	} 
	
	/**
	 * 删除文件
	 * @param filePath
	 * @return
	 */
	public boolean deleteFile(String filePath){
		 boolean flag=true;
		try {
			File deleteFile = new File(filePath);
			deleteFile.delete();
		} catch (RuntimeException e) {
			flag=false;
			LogMessage.logwrite(this.getClass(), e.getMessage());
		}
		return flag;
	}
	public String getSysSeparator() {
		return sysSeparator;
	}
	public void setSysSeparator(String sysSeparator) {
		this.sysSeparator = sysSeparator;
	}
	public String getFileUpload() {
		return fileUpload;
	}
	public void setFileUpload(String fileUpload) {
		this.fileUpload = fileUpload;
	}
	public String getTmpPath() {
		return tmpPath;
	}
	public void setTmpPath(String tmpPath) {
		this.tmpPath = tmpPath;
	}
	public String getShopPath() {
		return shopPath;
	}
	public void setShopPath(String shopPath) {
		this.shopPath = shopPath;
	}
	public String getImageType() {
		return imageType;
	}
	public void setImageType(String imageType) {
		this.imageType = imageType;
	}
	public void setUpload(String upload) {
		this.upload = upload;
	}
	public String getUpload() {
		return upload;
	}
 
		
		 
		 
		 
		
	 

	
}
